﻿Imports System.Data.SqlClient
Public Class LoaiNhaDAO
    Public Function GetItemID(ByVal ten As String) As LoaiNhaDTO
        Dim loainha As New LoaiNhaDTO
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from LOAINHA where TenLoaiNha = N'" + ten + "'"
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        da.Fill(data)
        Try
            loainha.MaLoaiNha = Convert.ToInt32(data.Rows(0)("MaLoaiNha").ToString())
            loainha.TenLoaiNha = data.Rows(0)("TenLoaiNha").ToString()
        Catch ex As Exception

        End Try

        Return loainha
    End Function
    Public Function SelectID(ByVal ma As String) As LoaiNhaDTO
        Dim loainha As New LoaiNhaDTO
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from LOAINHA where MaLoaiNha = " + ma
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        da.Fill(data)
        Try
            loainha.MaLoaiNha = Convert.ToInt32(data.Rows(0)("MaLoaiNha").ToString())
            loainha.TenLoaiNha = data.Rows(0)("TenLoaiNha").ToString()
        Catch ex As Exception

        End Try

        Return loainha
    End Function
    Public Function GetAll() As DataTable
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from LOAINHA"
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        Try
            da.Fill(data)
        Catch ex As Exception

        End Try
        Return data
    End Function
    Public Sub Insert(ByVal loai As LoaiNhaDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_InsertLOAINHA @TenLoaiNha,@MaLoaiNha"
            Dim para As SqlParameter = cmd.Parameters.Add("@TenLoaiNha", SqlDbType.NVarChar)
            para.Value = loai.TenLoaiNha
            para = cmd.Parameters.Add("@MaLoaiNha", SqlDbType.Int)
            para.Value = loai.MaLoaiNha
            cmd.ExecuteNonQuery()
            MessageBox.Show("Loại nhà đã được tạo thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Delete(ByVal loai As LoaiNhaDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_DeleteLOAINHA @MaLoaiNha"
            Dim para As SqlParameter = cmd.Parameters.Add("@MaLoaiNha", SqlDbType.NVarChar)
            para.Value = loai.MaLoaiNha
            cmd.ExecuteNonQuery()
            MessageBox.Show("Loại nhà đã được tạo thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Update(ByVal loai As LoaiNhaDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_UpdateLOAINHA @TenLoaiNha,@MaLoaiNha"
            Dim para As SqlParameter = cmd.Parameters.Add("@TenLoaiNha", SqlDbType.NVarChar)
            para.Value = loai.TenLoaiNha
            para = cmd.Parameters.Add("@MaLoaiNha", SqlDbType.Int)
            para.Value = loai.MaLoaiNha
            cmd.ExecuteNonQuery()
            MessageBox.Show("Loại nhà đã được cập nhật thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
